import QtQuick 2.0
import QtQuick.Controls 2.15

Item {
    property string updateMode
    property string webVersion
    property string link
    property string description

    Script {
        id: script

        property int mode: modeNone

        readonly property int modeNone: 0
        readonly property int modeCheckSelfUpdate: 1
        readonly property int modePerformSelfUpdate: 2

        function checkSelfUpdate() {
            ui.log("")
            ui.log(qsTr("Checking %APPNAME% for updates...").replace("%APPNAME%", appName))
            mode = modeCheckSelfUpdate
            execScript('selfupdate', 'check')
        }

        function performSelfUpdate() {
            ui.log("")
            ui.log(qsTr("Downloading and updating %APPNAME%...").replace("%APPNAME%", appName))
            mode = modePerformSelfUpdate
            execScript('selfupdate', ['update', link])
        }

        onFinished: {
            switch (mode) {
                case modeCheckSelfUpdate:
                    onCheckFinished(code, output)
                    break

                case modePerformSelfUpdate:
                    onPerformSelfUpdateFinished(code, output)
                    break
            }
        }

        function onCheckFinished(code, output) {
            if (code !== 0) {
                ui.log(output)
                return
            }

            [, updateMode, webVersion, link] = output.match(/.*PLVERSION\s([^\s]+)\s([^\s]+)\s([^\s]+)\n.*/)
            description = (output.match(/.*PLDESCRIPTION\s(.*)\n.*/)[1] || "").trim()

            if (!webVersion) {
                ui.log(output)
                ui.log(qsTr("Failed to get update version"))
                return
            }

            let installedVersion = version.split(/\s/)[0]
            if (installedVersion === webVersion) {
                ui.log(appName + " " + qsTr("is up to date"))
                return
            }

            ui.log(appName + " " + qsTr("update available") + " v" + webVersion)
            dialogConfirmUpdate.open()
        }

        function onPerformSelfUpdateFinished(code, output) {
            if (code === 0) {
                // force quit
                clientInstaller.installerState = clientInstaller.stateDone
                window.close()
                return
            }

            // error?
            ui.log("")
            ui.log(output)
        }

    }

    Dialog {
        id: dialogConfirmUpdate
        anchors.centerIn: parent
        title: window.title
        standardButtons: Dialog.Yes | Dialog.No
        modal: true
        focus: true
        width: block * 8

        Text {
            anchors.fill: parent
            textFormat: Text.RichText
            wrapMode: Text.WordWrap
            text: appName + " v" + webVersion + " " + qsTr("is available.")
                  + "<br/>" + qsTr("Do you wish to") + " " + qsTr(updateMode) + qsTr("?")
                  + "<br/><br/><b>" + qsTr("Release notes:") + "</b>" + description
        }

        onAccepted: {
            switch (updateMode) {
                case "update":
                    script.performSelfUpdate()
                    break

                case "download":
                    Qt.openUrlExternally(link)
                    break
            }
        }
    }

    function check() {
        script.checkSelfUpdate()
    }
}
